package com.xp.code.daoImpl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import oracle.jdbc.oracore.OracleType;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.xp.code.dao.IUnitInfoDao;
import com.xp.code.jpa.DefaultJPADAO;
import com.xp.code.util.CommonUtils;
import com.xp.code.vo.UnitInfoVO;
import com.xp.code.vo.UserInfoVO;
/**
 * 
 * @author HAHA
 *
 */
@Repository("unitInfoDao")
public class UnitInfoDao extends DefaultJPADAO implements IUnitInfoDao {

	public UnitInfoDao() {
		
	}

	public UnitInfoVO addUnitInfos(UnitInfoVO unitInfoVO) throws Exception {
		UnitInfoVO vo=null;
		CallableStatement cs = null;
		int FLAG = 0;
		String MSG = "添加成功";
		try {
			Session session = (Session) this.getEntityManager().getDelegate();
			Connection con = session.connection();
			cs = con.prepareCall("{CALL PACKAGEXPSYS_AUTH.AddUnitInfo(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			cs.setString(1, CommonUtils.getUUID());
			cs.setString(2, unitInfoVO.getVarname());
			cs.setString(3, unitInfoVO.getVarcode());
			cs.setString(4, unitInfoVO.getAddress());
			cs.setString(5, unitInfoVO.getParentid());
			cs.setString(6, unitInfoVO.getOwner());
			cs.setString(7, unitInfoVO.getConname());
			cs.setString(8, unitInfoVO.getConphone());
			cs.setString(9, unitInfoVO.getProvince());
			cs.setString(10, unitInfoVO.getCity());
			cs.setString(11, unitInfoVO.getValiddate());
			cs.setString(12, unitInfoVO.getRemark());
			cs.registerOutParameter(13, oracle.jdbc.driver.OracleTypes.INTEGER);
			cs.registerOutParameter(14, oracle.jdbc.driver.OracleTypes.VARCHAR);
			cs.executeUpdate();
			FLAG = cs.getInt(13);
			MSG = cs.getString(14);
			vo=new UnitInfoVO(FLAG,MSG);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cs != null) {
				cs.close();
			}
		}
		return vo;
	}
	
	public List<Object[]> getParentUnitInfo(String unitID)throws Exception{
		String sql="SELECT ID,VARNAME,VARCODE FROM TSYS_UNIT T1 WHERE T1.PARENTID=?";
		Query query=this.getEntityManager().createNativeQuery(sql);
		query.setParameter(1, unitID);
		return query.getResultList();
	}
}
